package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.dao.AdditionalCommodityDao;
import com.qdlc.p2p.dal.dto.AdditionalCommodity;
import com.qdlc.p2p.dal.model.AdditionalCommodityModel;

/**
 * Created by liaoxiangkai on 2015/12/9.
 */
@Repository("additionalCommodityDao")
public class AdditionalCommodityDaoImpl extends BaseDaoImpl<AdditionalCommodity> implements AdditionalCommodityDao {

    /**
     * 商品列表
     *
     * @param model
     * @return
     */
    @Override
    public PageDataList<AdditionalCommodityModel> list(AdditionalCommodityModel model) {
        QueryParam param = QueryParam.getInstance().addPage(model.getPage(), model.getRows());
        if (StringUtil.isNotBlank(model.getSearchName()))
            param.addParam("name", SearchFilter.Operators.LIKE, model.getSearchName().trim());
        param.addParam("isDelete", AdditionalCommodityModel.IS_DELETE_NO);
        PageDataList<AdditionalCommodity> pageList = findPageList(param);
        PageDataList<AdditionalCommodityModel> modelList = new PageDataList<AdditionalCommodityModel>();
        List<AdditionalCommodityModel> list = new ArrayList<AdditionalCommodityModel>();
        if (null != pageList.getList() && pageList.getList().size() > 0) {
            for (AdditionalCommodity entity : pageList.getList()) {
                AdditionalCommodityModel commodityModel = AdditionalCommodityModel.instance(entity);
                if (null != entity.getModifier()) {
                    commodityModel.setModifierName(entity.getModifier().getUserName());
                }
                commodityModel.setCreatorName(entity.getCreator().getUserName());
                list.add(commodityModel);
            }
        }
        modelList.setPage(pageList.getPage());
        modelList.setList(list);
        return modelList;
    }

    /**
     * 开启/禁用
     *
     * @param model
     * @return
     */
    @Override
    public int toggleStatus(AdditionalCommodityModel model) {
        String jpql = "update AdditionalCommodity set status = :status where id = :id";
        Query query = em.createQuery(jpql);
        query.setParameter("status", model.getStatus());
        query.setParameter("id", model.getId());
        return query.executeUpdate();
    }

    /**
     * 删除商品
     *
     * @param model
     * @return
     */
    @Override
    public int doDelete(AdditionalCommodityModel model) {
        String jpql = "update AdditionalCommodity set isDelete = :isDelete, modifier = :modifier, modifyTime = :modifyTime where id = :id";
        Query query = em.createQuery(jpql);
        query.setParameter("modifier", model.getModifier());
        query.setParameter("modifyTime", model.getModifyTime());
        query.setParameter("isDelete", AdditionalCommodityModel.IS_DELETE_YES);
        query.setParameter("id", model.getId());
        return query.executeUpdate();
    }

    /**
     * 更新发放数量
     * @return
     */
    @Override
    public int updateIssueNum(long id) {
        String jpql = "UPDATE AdditionalCommodity set issueNum = issueNum+1 WHERE issueNum+1 <= totalNum AND id = :id";
        return updateByJpql(jpql, new String[]{"id"}, new Object[]{id});
    }

    /**
     * 添加奖品时的商品列表json
     * @param category
     * @return
     */
    @Override
    public List<AdditionalCommodity> lotteryCommodityList(int category) {
        String jpql = "from AdditionalCommodity where status=:status and isDelete=:isDelete and category=:category";
        Query query = em.createQuery(jpql);
        query.setParameter("status", AdditionalCommodityModel.STATUS_OPEN);
        query.setParameter("isDelete", AdditionalCommodityModel.IS_DELETE_NO);
        query.setParameter("category", category);
        return query.getResultList();
    }
}
